local UIBase = require("view.UIBase")
local PopupMagicKey = class("PopupMagicKey", UIBase)
local MagicKeyDataHelper = require("data.helper.MagicKeyDataHelper")
local TextHelper = require("utils.TextHelper")
local Vector3 = require("utils.Vector3")
local GameUtils = require("utils.GameUtils")
local Color = require("utils.Color")

function PopupMagicKey:ctor(heroUnit)
    self._heroUnit = heroUnit
    local resource = {
        path = "Prefabs/View/MagicKey",
        name = "PopupMagickey",
        canvas = 3
    }
    PopupMagicKey.super.ctor(self, resource)
end

function PopupMagicKey:start()
    self._btnClose.onClick:AddListener(handler(self, self.close))
    self._btnUnload.onClick:AddListener(handler(self, self._onBtnUnloadClick))
    self._btnChange.onClick:AddListener(handler(self, self._onBtnChangeClick))
    self._btnUpgrade.onClick:AddListener(handler(self, self._onBtnUpgradeClick))
    self:updateView()
end

function PopupMagicKey:updateView()
    if self._heroUnit:getMagickeyId() > 0 then
        self._unit = G_UserData:getMagickey():getUnitById(self._heroUnit:getMagickeyId())
        self:_updateUI()
    else
        self:_updateEmpty()
    end
end

function PopupMagicKey:setUIVisible(empty)
    self._textEmpty.gameObject:SetActive(empty)
    self._textDesc.gameObject:SetActive(not empty)
    self._textEffect.gameObject:SetActive(not empty)
    self._imageMagic.gameObject:SetActive(not empty)
    self._attrRoot.gameObject:SetActive(not empty)
    self._btnUpgrade.gameObject:SetActive(not empty)
end

function PopupMagicKey:_updateEmpty()
    self._textName.text = Lang.get("lang_magickey_empty")
    self:setUIVisible(true)
end

function PopupMagicKey:removeAttr()
    local count = self._attrRoot.childCount
    for i = 0, count - 1, 1 do
        local child = self._attrRoot:GetChild(i)
        GameUtils.destroy(child.gameObject)
    end
end

function PopupMagicKey:_updateUI()
    self:setUIVisible(false)
    local config = self._unit:getConfig()
    self._textName.text = config.name
    self._textDesc.text = config.desc
    self._imageMagic.sprite = G_ResourceManager:loadSprite("MagickeyAltas", config.res)
    local effectCfg = require("config.fight_effect").get(config.effectId)
    self._textEffect.text = effectCfg.desc
    local attrs = MagicKeyDataHelper.getMagicKeyAttr(self._unit)
    local index = 1
    self:removeAttr()
    for id, value in pairs(attrs) do
        local name, value = TextHelper.getAttrBasicText(id, value)
        local text = G_UIUtil.createText(self._attrRoot, Vector3.new(0, index * 26, 0))
        text.text = name .. "+" .. value
        text.fontSize = 24
        text.color = Color.RED_COLOR
        index = index + 1
    end
end

function PopupMagicKey:_onBtnUnloadClick()
    local magicId = self._heroUnit:getMagickeyId()
    if magicId > 0 then
        G_UserData:getMagickey():clearMagickeyInHero(magicId)
        self:updateView()
        G_UIManager:showTip(Lang.get("lang_unload_success"))
    end
end

function PopupMagicKey:_onBtnChangeClick()
    local pop = require("view.magicKey.PopupMagickeyChoose").new(handler(self, self._onSelectedMagickey))
end

function PopupMagicKey:_onBtnUpgradeClick()
    local pop = require("view.magicKey.PopupMagicKeyUp").new(self._unit)
end

function PopupMagicKey:_onSelectedMagickey(id)
    if id > 0 then
        G_UserData:getMagickey():equipMagickeyToHero(id, self._heroUnit:getId())
        self:updateView()
        G_UIManager:showTip(Lang.get("lang_equip_success"))
    end
end

return PopupMagicKey
